package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import db.dao.Users;
import db.dao.UsersDAO;
/*修改学生密码*/
public class UpdateStuPwdAction extends ActionSupport{
	private String sid="";	//学生编号
	private String pwd="";	//原密码
	private String pwd1="";	//新密码
	private String pwd2="";	//确认密码
	
	public UpdateStuPwdAction() {
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getPwd1() {
		return pwd1;
	}

	public void setPwd1(String pwd1) {
		this.pwd1 = pwd1;
	}

	public String getPwd2() {
		return pwd2;
	}

	public void setPwd2(String pwd2) {
		this.pwd2 = pwd2;
	}
	
	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		//用于获取当前用户信息
		HttpSession session = request.getSession();
		String id=(String) session.getAttribute("username");
		//用于获取结果
		String result="";
		//查找用户
		UsersDAO uDao = new UsersDAO();		
		Users users = uDao.findById(id);
		if (users.getUsertype().equals("管理")) {
			//管理员修改需要学生编号
			users = uDao.findById(sid);
		}else {
			//学生修改需要判断原密码是否正确，不正确则弹窗提示
			if (!users.getUserpwd().equals(pwd)) {
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改失败!')</script>");
	            UsersDAO uDao1 = new UsersDAO();
	            Users users1 = uDao1.findById((String)session.getAttribute("username"));
	            if (users1.getUsertype().equals("管理")) {
					printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
				}else {
					printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
				}
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}
		}
		if (users!=null) {
			//判断新密码是否为空和是否一致
			if (!pwd1.equals("")&&pwd1.equals(pwd2)) {
				users.setUserpwd(pwd1);
				result = uDao.save(users);
			}
			if (result.equals("success")) {
				//成功弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改成功!')</script>");
	            UsersDAO uDao1 = new UsersDAO();
	            Users users1 = uDao1.findById((String)session.getAttribute("username"));
	            if (users1.getUsertype().equals("管理")) {
					printWriter.print("<script>window.location.href='/GroupProject/index.jsp'</script>");
				}else {
					printWriter.print("<script>window.location.href='/GroupProject/index.jsp'</script>");
				}
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}else {
				//失败弹窗提示
				response.setContentType("text/html;charset=UTF-8");
	            response.setCharacterEncoding("UTF-8");
	            PrintWriter printWriter = response.getWriter();
	            printWriter.print("<script>alert('修改失败!')</script>");
	            UsersDAO uDao1 = new UsersDAO();
	            Users users1 = uDao1.findById((String)session.getAttribute("username"));
	            if (users1.getUsertype().equals("管理")) {
					printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
				}else {
					printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
				}
	            printWriter.flush();
	            printWriter.close();
	            response.setHeader("Pragma","No-cache");   
	            response.setHeader("Cache-Control","no-cache");  
	            response.setHeader("Cache-Control", "no-store");  
	            response.setDateHeader("Expires", 0);
				return null;
			}
		}else {
			//未查到用户 则弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('未查询到该学生!')</script>");
            UsersDAO uDao1 = new UsersDAO();
            Users users1 = uDao1.findById((String)session.getAttribute("username"));
            if (users1.getUsertype().equals("管理")) {
				printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
			}else {
				printWriter.print("<script>window.location.href='/GroupProject/student.jsp'</script>");
			}
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
